package com.ruoyi.business.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.vo.TEducationInfoVo;
import com.ruoyi.business.service.ITEducationInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 教育风采Controller
 *
 * @author Tellsea
 * @date 2024-01-17
 */
@Api(tags = "教育风采Controller")
@RestController
@RequestMapping("/business/educationInfo")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TEducationInfoController extends BaseController {

    private final ITEducationInfoService tEducationInfoService;

    @ApiOperation("查询教育风采列表")
    //@PreAuthorize("@ss.hasPermi('business:educationInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<TEducationInfoVo> list(TEducationInfoVo entity) {
        return tEducationInfoService.queryList(entity);
    }

    @ApiOperation("查询教育风采所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TEducationInfoVo entity) {
        return AjaxResult.success("查询成功", tEducationInfoService.queryAll(entity));
    }

    @ApiOperation("导出教育风采列表")
    @PreAuthorize("@ss.hasPermi('business:educationInfo:export')")
    @Log(title = "教育风采", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TEducationInfoVo entity) {
        List<TEducationInfoVo> list = tEducationInfoService.queryAll(entity);
        ExcelUtil<TEducationInfoVo> util = new ExcelUtil<>(TEducationInfoVo.class);
        util.exportExcel(response, list, "教育风采数据");
    }

    @ApiOperation("获取教育风采详细信息")
    //@PreAuthorize("@ss.hasPermi('business:educationInfo:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TEducationInfoVo entity = tEducationInfoService.queryById(id);
        if (Objects.isNull(entity.getReadNumber())) {
            entity.setReadNumber(0L);
        } else {
            entity.setReadNumber(entity.getReadNumber() + 1);
        }
        tEducationInfoService.updateById(entity);
        return AjaxResult.success("查询成功", entity);
    }

    @ApiOperation("新增教育风采")
    @PreAuthorize("@ss.hasPermi('business:educationInfo:add')")
    @Log(title = "教育风采", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TEducationInfoVo entity) {
        entity.setReadNumber(0L);
        entity.setRegisterDeptId(SecurityUtils.getLoginUser().getRegisterDeptId());
        return toAjax(tEducationInfoService.save(entity));
    }

    @ApiOperation("修改教育风采")
    @PreAuthorize("@ss.hasPermi('business:educationInfo:edit')")
    @Log(title = "教育风采", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TEducationInfoVo entity) {
        if (ObjectUtil.isEmpty(entity.getRegisterDeptId())) {
            entity.setRegisterDeptId(SecurityUtils.getLoginUser().getRegisterDeptId());
        }
        return toAjax(tEducationInfoService.updateById(entity));
    }

    @ApiOperation("删除教育风采")
    @PreAuthorize("@ss.hasPermi('business:educationInfo:remove')")
    @Log(title = "教育风采", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tEducationInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
